package order.ordertop.view;

import java.awt.GridLayout;
import java.sql.Date;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import model.Employee;
import model.MainModel;
import model.Order;
import model.Role;
import net.proteanit.sql.DbUtils;
import order.ordertop.model.OrderTopModel;
import admin.employee.view.EmployeeModel;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class OrderTopView extends JPanel {
	private static final long serialVersionUID = 1L;
	private final String NEW = "New";
	private final String DELETE = "Delete";
	
	private JTable tableOrderTop = null;
	private OrderTopModel orderTopModel;
	
	JPanel orderTopButtonPanel;
	JPanel orderTopTextPanel;
	JPanel orderTopTablePanel;
	
	private int orderId;
	
	private JButton orderTopNewB;
	private JButton orderTopDeleteB;
	
	private JTextField orderTopOrderIdTF;
	private JTextField orderTopDateTF;
	private JTextField orderTopTotalPriceTF;
	
	private ButtonActionListener buttonActionListener;
	private TableMouseAdapter tableMouseAdapter;

	public OrderTopView() {
		this.setLayout(new BorderLayout());
		buttonActionListener = new ButtonActionListener();
		tableMouseAdapter = new TableMouseAdapter();
		orderTopModel = new OrderTopModel();
		initConponents();
		update();		
	}
	
	public void update(){
		updateView();
	}

	private void updateView() {
		ResultSet rs = null;
		rs = orderTopModel.orderTopDao.getOrderTopResultSet();
		if(rs != null){
			tableOrderTop.setModel(DbUtils.resultSetToTableModel(rs));
		}else { JOptionPane.showMessageDialog(null, "orderTopView rs fault"); }		
	}
	
	private void initConponents() {
		orderTopButtonPanel = new JPanel();
		orderTopTextPanel = new JPanel();
		orderTopTablePanel = new JPanel();		
		
		//***ButtonPanel		
		this.add(orderTopButtonPanel, BorderLayout.SOUTH);
		orderTopNewB = new JButton(NEW); //new button
		orderTopNewB.setActionCommand(NEW);
		orderTopNewB.addActionListener(buttonActionListener);
		orderTopButtonPanel.add(orderTopNewB);

		orderTopDeleteB = new JButton(DELETE);
		orderTopDeleteB.setActionCommand(DELETE);
		orderTopDeleteB.addActionListener(buttonActionListener);
		orderTopButtonPanel.add(orderTopDeleteB);

		orderTopButtonPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("orderTop Actions"));
		//***end of ButtonPanel
        
		// ***TextPanel
        JPanel t = new JPanel(new BorderLayout());
        t.add(orderTopTextPanel, BorderLayout.NORTH);
		orderTopTextPanel.setLayout(new GridLayout(3, 2));
		JLabel label = new JLabel("Order ID :");
		orderTopTextPanel.add(label);
		orderTopOrderIdTF = new JTextField();
		orderTopTextPanel.add(orderTopOrderIdTF);
		this.add(t, BorderLayout.WEST);
		
		label = new JLabel("Date & Time :");
		orderTopTextPanel.add(label);
		orderTopDateTF = new JTextField();
		orderTopTextPanel.add(orderTopDateTF);
		
		label = new JLabel("Total Price :");
		orderTopTextPanel.add(label);
		orderTopTotalPriceTF = new JTextField();
		orderTopTextPanel.add(orderTopTotalPriceTF);
		orderTopTextPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Main Order Detail"));
		// ***end of TextPanel
		
		// ***orderTopTablePanel
		this.add(orderTopTablePanel, BorderLayout.CENTER);		
		tableOrderTop = new JTable();
		tableOrderTop.addMouseListener(tableMouseAdapter);
		JScrollPane scrollPane = new JScrollPane(tableOrderTop);
		orderTopTablePanel.add(scrollPane);
		//orderTopTablePanel.add(tableOrderTop);
		orderTopTablePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Main Order Table"));
		// ***end of orderTopTablePanel
		edComponents(orderTopTextPanel, false);// set disable orderTopTextPanel components at the beginning
	}
	
	//sets enable or disable all components in a container
	private void edComponents(Container container, boolean b) {
		Component[] components = container.getComponents();
		for (Component component : components) {
			component.setEnabled(b);
			if (component instanceof Container) {
				edComponents((Container)component, b);
			}
		}			
	}
	
	class TableMouseAdapter extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (tableOrderTop.isEnabled()) {// only work when tableOrderTop is enabled
				System.out.println("mouse clicked on table");
				try {
					int row = tableOrderTop.getSelectedRow();
					orderId = (int) tableOrderTop.getModel().getValueAt(row, 0);
					setTableRowToTextField(orderId);
				} catch (Exception e2) {
					System.out.println("Mouse listener Exception!");
				}
			}
		}
	}
	 
	class ButtonActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();
			switch (cmd) {
			case NEW:
				System.out.println("NEWtbn in orderTop view");
				orderTopModel.orderTopDao.newOrderTop(MainModel.getUser().getSsn());
				updateView();
				break;

			case DELETE:
				if (!orderTopOrderIdTF.getText().isEmpty()) {// only work if there is to delete
					System.out.println("DELETEbtn in orderTop view");
					orderTopModel.orderTopDao.deleteOrderTop(orderTopOrderIdTF.getText());
					updateView();
				} else {
					JOptionPane.showMessageDialog(null, "please click first on some item!!");
				}
				break;
			}
		}
	}// end ButtonActionListener class

	private void setTableRowToTextField(int orderId) {
		Order tmpOrder = orderTopModel.orderTopDao.getOrderTop(orderId);
		if (tmpOrder != null) {
			System.out.println(orderId);
			orderTopOrderIdTF.setText(Integer.toString(tmpOrder.getOrderId()));
			orderTopDateTF.setText((tmpOrder.getDate().toString()));
			orderTopTotalPriceTF.setText(Double.toString(tmpOrder.getTotalPrice()));
		}

	}

	private void emptyTextField() {
		orderTopOrderIdTF.setText("");
		orderTopDateTF.setText("");
		orderTopTotalPriceTF.setText("");
	}

}
